package cn.raven.serve.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.pet.domain.PetDetail;
import cn.raven.serve.domain.ProductDetail;
import cn.raven.serve.service.IProductService;
import cn.raven.serve.domain.Product;
import cn.raven.serve.query.ProductQuery;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;


    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    //@PreAuthorize(name = "保存product",sn = "product:save")
    public AjaxResult save(@RequestBody Product product){
        try {
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.add(product);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    //@PreAuthorize(name = "删除product",sn = "product:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            productService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    //@PreAuthorize(name = "id查询product",sn = "product:id")
    public Product findById(@PathVariable("id")Long id)
    {
        return productService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> findAll(){

        return productService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    //@PreAuthorize(name = "product列表",sn = "product:list")
    public PageList<Product> queryData(@RequestBody ProductQuery query)
    {
        return productService.queryData(query);
    }
    /**
    * @Title: detail
    * @Description: 服务详情回显
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 10:01
    * @Parameters: [id]
    * @Return cn.raven.pet.domain.PetDetail
      */
    @GetMapping("/detail/{id}")
    public ProductDetail detail(@PathVariable("id") Long id){
        return productService.findByPetId(id);
    }

    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 10:20
    * @Parameters: [ids]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PatchMapping("/batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            productService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }

    /**
    * @Title: onsale
    * @Description: 批量上架服务
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 10:24
    * @Parameters: [ids]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            productService.onsale(ids);
            return AjaxResult.me();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }

    /**
     * @Title: offsale
     * @Description: 服务批量下架
     * @Author: king
     * @Version: 1.0
     * @Date:  2022/7/16 16:41
     * @Parameters: [ids]
     * @Return cn.raven.basic.utils.AjaxResult
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            productService.offsale(ids);
            return AjaxResult.me();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }

}
